// components/play-bar/index.js
import {playerStore} from '../../store/index'
const playModeNames = ["order","repeat","random"]
Component({
 
  properties: {
    
  },

  data: {
    currentSong:{},
    currentLyricText:"",
    isPlaying:"",
    show:false,
    canPlaySongList: [],
    canPlaySongCurrentIndex: 0,
    playModeName:"order"
  },

  lifetimes:{
    // 在组件实例进入页面节点树时执行
    attached:function(){
      this.setupPlayerStoreListener()
    },
    
  },

  methods: {
     //==========================点击小播放器=========================
      handlePlayBtnClick:function(event){
        playerStore.dispatch("changeMusicPlayStatusAction",!this.data.isPlaying)
        playerStore.onState("isPlaying",(value)=>{
          this.setData({isPlaying:value})
        })
        console.log(this.data.isPlaying);
      },

      handlePlayBarClick:function(){
        wx.navigateTo({
          url: '/pages/music-player/index?id=' + this.data.currentSong.id,
        })
      },
      
      //=========================获取store中的共享数据==============
      setupPlayerStoreListener:function(){
        //播放器的监听
        playerStore.onStates(["currentSong","isPlaying","currentLyricText"],({currentSong ,isPlaying ,currentLyricText})=>{
          if(currentSong) this.setData({currentSong})
          if(isPlaying !== undefined) this.setData({ 
            isPlaying ,
            playAnimState: isPlaying ? "running":"paused"
          })
          if(currentLyricText) this.setData({currentLyricText})
        })
        playerStore.onState("playListSongs", (res) => {
          this.setData({
            canPlaySongList: res
          })
        })
    
        playerStore.onState("playListIndex", (res) => {
          this.setData({
            canPlaySongCurrentIndex: res
          })
        })
          //监听播放模式相关的数据
        playerStore.onStates(["playModeIndex","isPlaying"],({playModeIndex, isPlaying})=>{
          if(playModeIndex !== undefined){
            this.setData({
              playModeIndex,
              playModeName:playModeNames[playModeIndex]})
          }
          if(isPlaying !== undefined){
            this.setData({
              isPlaying,
              playingName :isPlaying? "pause" : "resume"
            })
          }
        })

    
      },
      //换播放模式
      handleModeBtnClick:function(){
        let playModeIndex = this.data.playModeIndex + 1
        if(playModeIndex === 3) playModeIndex = 0
    
        playerStore.setState("playModeIndex",playModeIndex)
      },

      handleSelectBtnClick: function (event) {
        const index = event.currentTarget.dataset.index
        playerStore.dispatch("selectNewMusicAction", index)
      },
      
      onClickShow:function(){
        this.setData({
          show:true
        })
        playerStore.onState("playListSongs", (res) => {
          this.setData({
            canPlaySongList: res
          })
        })
    
        playerStore.onState("playListIndex", (res) => {
          this.setData({
            canPlaySongCurrentIndex: res
          })
        })
          //监听播放模式相关的数据
        playerStore.onStates(["playModeIndex","isPlaying"],({playModeIndex, isPlaying})=>{
          if(playModeIndex !== undefined){
            this.setData({
              playModeIndex,
              playModeName:playModeNames[playModeIndex]})
          }
          if(isPlaying !== undefined){
            this.setData({
              isPlaying,
              playingName :isPlaying? "pause" : "resume"
            })
          }
        })
      
      },
      onClickHide:function(){
        this.setData({
          show:false
        })
      }

  }
})
